﻿using System;
using System.Collections.Generic;
using System.Linq;
using GUITree;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Studio
{
	// Token: 0x02000534 RID: 1332
	public class TreeNodeCtrl : MonoBehaviour, IPointerDownHandler, IEventSystemHandler
	{
		// Token: 0x17000418 RID: 1048
		// (get) Token: 0x0600220A RID: 8714 RVA: 0x000CE5F8 File Offset: 0x000CC7F8
		// (set) Token: 0x0600220B RID: 8715 RVA: 0x000CE620 File Offset: 0x000CC820
		public TreeNodeObject selectNode
		{
			get
			{
				TreeNodeObject[] selectNodes = this.selectNodes;
				return (selectNodes.Length == 0) ? null : selectNodes[0];
			}
			set
			{
				this.SetSelectNode(value);
			}
		}

		// Token: 0x17000419 RID: 1049
		// (get) Token: 0x0600220C RID: 8716 RVA: 0x000CE62C File Offset: 0x000CC82C
		public TreeNodeObject[] selectNodes
		{
			get
			{
				return this.hashSelectNode.ToArray<TreeNodeObject>();
			}
		}

		// Token: 0x1700041A RID: 1050
		// (get) Token: 0x0600220D RID: 8717 RVA: 0x000CE63C File Offset: 0x000CC83C
		public ObjectCtrlInfo[] selectObjectCtrl
		{
			get
			{
				List<ObjectCtrlInfo> list = new List<ObjectCtrlInfo>();
				foreach (TreeNodeObject key in this.hashSelectNode)
				{
					ObjectCtrlInfo item = null;
					if (Singleton<Studio>.Instance.dicInfo.TryGetValue(key, out item))
					{
						list.Add(item);
					}
				}
				return list.ToArray();
			}
		}

		// Token: 0x0600220E RID: 8718 RVA: 0x000CE6C0 File Offset: 0x000CC8C0
		public TreeNodeObject AddNode(string _name, TreeNodeObject _parent = null)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.m_ObjectNode);
			gameObject.SetActive(true);
			gameObject.transform.SetParent(this.m_ObjectRoot.transform, false);
			TreeNodeObject component = gameObject.GetComponent<TreeNodeObject>();
			component.textName = _name;
			if (_parent)
			{
				component.SetParent(_parent);
			}
			else
			{
				this.m_TreeNodeObject.Add(component);
			}
			return component;
		}

		// Token: 0x0600220F RID: 8719 RVA: 0x000CE72C File Offset: 0x000CC92C
		public bool AddNode(TreeNodeObject _node)
		{
			if (_node == null || _node.parent != null)
			{
				return false;
			}
			if (this.m_TreeNodeObject.Contains(_node))
			{
				return false;
			}
			this.m_TreeNodeObject.Add(_node);
			return true;
		}

		// Token: 0x06002210 RID: 8720 RVA: 0x000CE778 File Offset: 0x000CC978
		public void RemoveNode(TreeNodeObject _node)
		{
			if (_node == null || _node.parent == null)
			{
				return;
			}
			this.m_TreeNodeObject.Remove(_node);
		}

		// Token: 0x06002211 RID: 8721 RVA: 0x000CE7A8 File Offset: 0x000CC9A8
		public bool CheckNode(TreeNodeObject _node)
		{
			return !(_node == null) && this.m_TreeNodeObject.Contains(_node);
		}

		// Token: 0x06002212 RID: 8722 RVA: 0x000CE7C4 File Offset: 0x000CC9C4
		public void DeleteNode(TreeNodeObject _node)
		{
			if (!_node.enableDelete)
			{
				return;
			}
			_node.SetParent(null);
			this.m_TreeNodeObject.Remove(_node);
			this.DeleteNodeLoop(_node);
			if (this.m_TreeNodeObject.Count == 0)
			{
				this.scrollRect.verticalNormalizedPosition = 1f;
			}
		}

		// Token: 0x06002213 RID: 8723 RVA: 0x000CE81C File Offset: 0x000CCA1C
		public void DeleteAllNode()
		{
			foreach (TreeNodeObject node in this.m_TreeNodeObject)
			{
				this.DeleteAllNodeLoop(node);
			}
			this.m_TreeNodeObject.Clear();
			this.hashSelectNode.Clear();
			this.scrollRect.verticalNormalizedPosition = 1f;
			this.scrollRect.horizontalNormalizedPosition = 0f;
		}

		// Token: 0x06002214 RID: 8724 RVA: 0x000CE8B0 File Offset: 0x000CCAB0
		public TreeNodeObject GetNode(int _index)
		{
			int count = this.m_TreeNodeObject.Count;
			if (count == 0)
			{
				return null;
			}
			return (_index < 0 || _index >= count) ? null : this.m_TreeNodeObject[_index];
		}

		// Token: 0x06002215 RID: 8725 RVA: 0x000CE8F4 File Offset: 0x000CCAF4
		public void SetParent(TreeNodeObject _node, TreeNodeObject _parent)
		{
			if (_node == null)
			{
				return;
			}
			if (!_node.enableChangeParent)
			{
				return;
			}
			if (this.CheckNode(_node) && _parent == null)
			{
				return;
			}
			if (!_node.SetParent(_parent))
			{
				return;
			}
			this.RefreshHierachy();
			this.m_TreeRoot.Invoke("SetDirty", 0f);
			if (this.onParentage != null)
			{
				this.onParentage(_parent, _node);
			}
		}

		// Token: 0x06002216 RID: 8726 RVA: 0x000CE974 File Offset: 0x000CCB74
		public void RefreshHierachy()
		{
			foreach (TreeNodeObject source in this.m_TreeNodeObject)
			{
				this.RefreshHierachyLoop(source, 0, true);
				this.RefreshVisibleLoop(source);
			}
		}

		// Token: 0x06002217 RID: 8727 RVA: 0x000CE9DC File Offset: 0x000CCBDC
		public void SetParent()
		{
			TreeNodeObject[] selectNodes = this.selectNodes;
			for (int i = 1; i < selectNodes.Length; i++)
			{
				this.SetParent(selectNodes[i], selectNodes[0]);
			}
			this.SelectSingle(null);
		}

		// Token: 0x06002218 RID: 8728 RVA: 0x000CEA18 File Offset: 0x000CCC18
		public void RemoveNode()
		{
			TreeNodeObject[] selectNodes = this.selectNodes;
			for (int i = 0; i < selectNodes.Length; i++)
			{
				this.SetParent(selectNodes[i], null);
			}
			this.SelectSingle(null);
		}

		// Token: 0x06002219 RID: 8729 RVA: 0x000CEA54 File Offset: 0x000CCC54
		public void DeleteNode()
		{
			TreeNodeObject[] selectNodes = this.selectNodes;
			for (int i = 0; i < selectNodes.Length; i++)
			{
				this.DeleteNode(selectNodes[i]);
			}
			this.SelectSingle(null);
		}

		// Token: 0x0600221A RID: 8730 RVA: 0x000CEA8C File Offset: 0x000CCC8C
		public void CopyChangeAmount()
		{
			TreeNodeObject[] selectNodes = this.selectNodes;
			ObjectCtrlInfo objectCtrlInfo = null;
			if (!Singleton<Studio>.Instance.dicInfo.TryGetValue(selectNodes[0], out objectCtrlInfo))
			{
				return;
			}
			List<TreeNodeCommand.MoveCopyInfo> list = new List<TreeNodeCommand.MoveCopyInfo>();
			for (int i = 1; i < selectNodes.Length; i++)
			{
				ObjectCtrlInfo objectCtrlInfo2 = null;
				if (Singleton<Studio>.Instance.dicInfo.TryGetValue(selectNodes[i], out objectCtrlInfo2))
				{
					list.Add(new TreeNodeCommand.MoveCopyInfo(objectCtrlInfo2.objectInfo.dicKey, objectCtrlInfo2.objectInfo.changeAmount, objectCtrlInfo.objectInfo.changeAmount));
				}
			}
			Singleton<UndoRedoManager>.Instance.Do(new TreeNodeCommand.MoveCopyCommand(list.ToArray()));
			this.SelectSingle(null);
		}

		// Token: 0x0600221B RID: 8731 RVA: 0x000CEB3C File Offset: 0x000CCD3C
		private void RefreshHierachyLoop(TreeNodeObject _source, int _indent, bool _visible)
		{
			_source.transform.SetAsLastSibling();
			_source.treeNode.indent = _indent;
			if (_source.gameObject.activeSelf != _visible)
			{
				_source.gameObject.SetActive(_visible);
			}
			int childCount = _source.childCount;
			if (_visible)
			{
				_visible = (_source.treeState == TreeNodeObject.TreeState.Open);
			}
			for (int i = 0; i < childCount; i++)
			{
				this.RefreshHierachyLoop(_source.child[i], _indent + 1, _visible);
			}
		}

		// Token: 0x0600221C RID: 8732 RVA: 0x000CEBC0 File Offset: 0x000CCDC0
		private void RefreshVisibleLoop(TreeNodeObject _source)
		{
			if (!_source.visible)
			{
				_source.visible = _source.visible;
				return;
			}
			int childCount = _source.childCount;
			for (int i = 0; i < childCount; i++)
			{
				this.RefreshVisibleLoop(_source.child[i]);
			}
		}

		// Token: 0x0600221D RID: 8733 RVA: 0x000CEC10 File Offset: 0x000CCE10
		private void DeleteNodeLoop(TreeNodeObject _node)
		{
			if (_node == null)
			{
				return;
			}
			int childCount = _node.childCount;
			for (int i = 0; i < childCount; i++)
			{
				this.DeleteNodeLoop(_node.child[i]);
			}
			UnityEngine.Object.Destroy(_node.gameObject);
			if (this.onDelete != null)
			{
				this.onDelete(_node);
			}
		}

		// Token: 0x0600221E RID: 8734 RVA: 0x000CEC78 File Offset: 0x000CCE78
		private void DeleteAllNodeLoop(TreeNodeObject _node)
		{
			if (_node == null)
			{
				return;
			}
			int childCount = _node.childCount;
			for (int i = 0; i < childCount; i++)
			{
				this.DeleteAllNodeLoop(_node.child[i]);
			}
			UnityEngine.Object.DestroyImmediate(_node.gameObject);
		}

		// Token: 0x0600221F RID: 8735 RVA: 0x000CECC8 File Offset: 0x000CCEC8
		private void SetSelectNode(TreeNodeObject _node)
		{
			bool flag = Input.GetKey(KeyCode.LeftControl) | Input.GetKey(KeyCode.RightControl);
			TreeNodeCtrl.Calc calc = TreeNodeCtrl.Calc.None;
			if (this.selectNode && Input.GetKey(KeyCode.P))
			{
				calc = TreeNodeCtrl.Calc.Attach;
			}
			else if (Input.GetKey(KeyCode.O))
			{
				calc = TreeNodeCtrl.Calc.Detach;
			}
			else if (Input.GetKey(KeyCode.X))
			{
				calc = TreeNodeCtrl.Calc.Copy;
			}
			switch (calc)
			{
			case TreeNodeCtrl.Calc.Attach:
				if (flag)
				{
					this.hashSelectNode.Add(_node);
					this.SetParent();
				}
				else
				{
					this.SetParent(this.selectNode, _node);
				}
				this.SelectSingle(_node);
				return;
			case TreeNodeCtrl.Calc.Detach:
				if (flag)
				{
					this.hashSelectNode.Add(_node);
					foreach (TreeNodeObject node in this.hashSelectNode)
					{
						this.SetParent(node, null);
					}
				}
				else
				{
					this.SetParent(_node, null);
				}
				this.SelectSingle(_node);
				return;
			case TreeNodeCtrl.Calc.Copy:
				if (flag)
				{
					this.hashSelectNode.Add(_node);
					if (this.hashSelectNode.Count > 1)
					{
						this.CopyChangeAmount();
					}
				}
				this.SelectSingle(_node);
				return;
			}
			if (flag)
			{
				this.AddSelectNode(_node);
			}
			else
			{
				this.SelectSingle(_node);
			}
		}

		// Token: 0x06002220 RID: 8736 RVA: 0x000CEE5C File Offset: 0x000CD05C
		public void SelectSingle(TreeNodeObject _node)
		{
			foreach (TreeNodeObject treeNodeObject in this.hashSelectNode)
			{
				treeNodeObject.OnDeselect();
			}
			this.hashSelectNode.Clear();
			this.AddSelectNode(_node);
		}

		// Token: 0x06002221 RID: 8737 RVA: 0x000CEECC File Offset: 0x000CD0CC
		private void AddSelectNode(TreeNodeObject _node)
		{
			if (_node == null)
			{
				return;
			}
			this.hashSelectNode.Add(_node);
			if (this.onSelect != null && this.hashSelectNode.Count == 1)
			{
				this.onSelect(_node);
			}
			else if (this.onSelectMultiple != null && this.hashSelectNode.Count > 1)
			{
				this.onSelectMultiple();
			}
			_node.colorSelect = ((this.hashSelectNode.Count != 1) ? Utility.ConvertColor(94, 139, 100) : Utility.ConvertColor(91, 164, 82));
			ObjectCtrlInfo objectCtrlInfo = null;
			if (Singleton<Studio>.Instance.dicInfo.TryGetValue(_node, out objectCtrlInfo))
			{
				Singleton<GuideObjectManager>.Instance.selectObject = objectCtrlInfo.guideObject;
			}
			else
			{
				Singleton<GuideObjectManager>.Instance.selectObject = null;
			}
		}

		// Token: 0x06002222 RID: 8738 RVA: 0x000CEFB8 File Offset: 0x000CD1B8
		public void OnPointerDown(PointerEventData eventData)
		{
			SortCanvas.select = this.canvas;
		}

		// Token: 0x06002223 RID: 8739 RVA: 0x000CEFC8 File Offset: 0x000CD1C8
		private void Start()
		{
			this.m_ObjectRoot.transform.localPosition = Vector3.zero;
		}

		// Token: 0x04002341 RID: 9025
		[SerializeField]
		protected GameObject m_ObjectNode;

		// Token: 0x04002342 RID: 9026
		[SerializeField]
		protected GameObject m_ObjectRoot;

		// Token: 0x04002343 RID: 9027
		[SerializeField]
		protected TreeRoot m_TreeRoot;

		// Token: 0x04002344 RID: 9028
		protected List<TreeNodeObject> m_TreeNodeObject = new List<TreeNodeObject>();

		// Token: 0x04002345 RID: 9029
		protected HashSet<TreeNodeObject> hashSelectNode = new HashSet<TreeNodeObject>();

		// Token: 0x04002346 RID: 9030
		public Action<TreeNodeObject, TreeNodeObject> onParentage;

		// Token: 0x04002347 RID: 9031
		public Action<TreeNodeObject> onDelete;

		// Token: 0x04002348 RID: 9032
		public Action<TreeNodeObject> onSelect;

		// Token: 0x04002349 RID: 9033
		public Action onSelectMultiple;

		// Token: 0x0400234A RID: 9034
		[SerializeField]
		private Canvas canvas;

		// Token: 0x0400234B RID: 9035
		[SerializeField]
		private ScrollRect scrollRect;

		// Token: 0x02000535 RID: 1333
		private enum Calc
		{
			// Token: 0x0400234D RID: 9037
			None,
			// Token: 0x0400234E RID: 9038
			Attach,
			// Token: 0x0400234F RID: 9039
			Detach,
			// Token: 0x04002350 RID: 9040
			Delete,
			// Token: 0x04002351 RID: 9041
			Copy
		}
	}
}
